import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

public class CreateInstance {
    public static void main(String[] args) {
        /*try {
            // 加载 Person class 文件到内存，创建一个 Class 类型的对象
            Class<?> personClass = Class.forName("Person");

            // Object person = personClass.newInstance();
            Person person = (Person) personClass.newInstance();

            System.out.println(person.toString());

            person.setName("abc");
            person.setAge(18);

            System.out.println(person.toString());

        } catch (ClassNotFoundException | IllegalAccessException | InstantiationException e) {
            e.printStackTrace();
        }*/


        try {
            // 加载 Person class 文件到内存，创建一个 Class 类型的对象
            Class<?> personClass = Class.forName("Person");

            Constructor<?> declaredConstructor = personClass.getDeclaredConstructor(String.class, int.class);
            // Object person = declaredConstructor.newInstance("abc", 18);
            Person person = (Person) declaredConstructor.newInstance("abc", 18);

            System.out.println(person.toString());
        } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException | InstantiationException | InvocationTargetException e) {
            e.printStackTrace();
        }
    }
}
